﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using PolePositionMobileUtilityGeneral;

namespace PolePositionMobileUtilityData
{
    public class DSetting
    {
        public static DataTable GetAll()
        {
            DataTable _Dt = new DataTable();
            SqlCeConnection _Con = null;
            SqlCeDataAdapter _Da = null;
            StringBuilder _sql = new StringBuilder();
            try
            {
                _Con = Utils.GetConnection();
                #region Query
                _sql.AppendLine("Select * ");
                _sql.AppendLine("FROM tb_Settings ");
                #endregion Query
                _Da = new SqlCeDataAdapter(_sql.ToString(), _Con);
                _Da.SelectCommand.CommandType = CommandType.Text;
                _Da.Fill(_Dt);
                return _Dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        public static void DeleteByType(int type, SqlCeTransaction tr)
        {
            StringBuilder _Sql = new StringBuilder(); ;
            SqlCeCommand _Cmd;
            #region Query
            _Sql.AppendLine("delete tb_Settings where SettingKey = @SettingKey ");
            #endregion Query
            _Cmd = new SqlCeCommand(_Sql.ToString(), (SqlCeConnection)tr.Connection);
            _Cmd.Transaction = tr;
            #region Parameters
            _Cmd.Parameters.Add(Utils.CreateIntParameter("@SettingKey", ParameterDirection.Input, false, type));
            #endregion Parameters
            _Cmd.ExecuteNonQuery();
        }

        public static void Insert(int type, string name, int value, SqlCeTransaction tr)
        {
            StringBuilder _Sql = new StringBuilder(); ;
            SqlCeCommand _Cmd;
            #region Query
            _Sql.AppendLine("Insert into tb_Settings (SettingKey,SettingName,SettingValue) ");
            _Sql.AppendLine(" Values(@SettingKey,@SettingName,@SettingValue)");
            #endregion Query
            _Cmd = new SqlCeCommand(_Sql.ToString(), (SqlCeConnection)tr.Connection);
            _Cmd.Transaction = tr;
            #region Parameters
            _Cmd.Parameters.Add(Utils.CreateIntParameter("@SettingKey", ParameterDirection.Input, false, type));
            _Cmd.Parameters.Add(Utils.CreateIntParameter("@SettingValue", ParameterDirection.Input, false, value));
            _Cmd.Parameters.Add(Utils.CreateNVarCharParameter("@SettingName", 255, ParameterDirection.Input, false, name));
            #endregion Parameters
            _Cmd.ExecuteNonQuery();
  
        }
    }
}
